package com.demo.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author wangfengchen
 */
public class RegexUtil {

    /**
     * 正则校验整数，且小数点只能是2位，整数部分最长18位，可以为负数
     * @param str
     * @return
     */
    public static boolean isNumberOrDecimalOrMinus(String str){
        Pattern pattern = Pattern.compile("^(([-]?[1-9]{1}\\d{0,17})|([-]?[0]{1}))(\\.(\\d){0,2})?$");
        Matcher match = pattern.matcher(str);
        return match.matches();
    }

    /**
     * 正则校验整数，且小数点只能是2位，整数部分最长18位
     * @param str
     * @return
     */
    public static boolean isNumberOrDecimal(String str){
        Pattern pattern = Pattern.compile("^(([1-9]{1}\\d{0,17})|([0]{1}))(\\.(\\d){0,2})?$");
        Matcher match = pattern.matcher(str);
        return match.matches();
    }

    /**
     * 正则校验 1-13位字母或数字或组合
     * @param str
     * @return
     */
    public static boolean isNumberOrLetter(String str){
        Pattern pattern = Pattern.compile("^[0-9a-zA-Z]{1,13}$");
        Matcher match = pattern.matcher(str);
        return match.matches();
    }

    /**
     * 正则校验 n位数字
     * @param str
     * @return
     */
    public static boolean isNumber(String str, int num){
        if(num < 1){
            return false;
        }
        Pattern pattern = Pattern.compile("^\\d{" + num + "}$");
        Matcher match = pattern.matcher(str);
        return match.matches();
    }

    /**
     * 正则校验大于0正整数
     * @param str
     * @return
     */
    public static boolean isPositiveNumber(String str){
        Pattern pattern = Pattern.compile("^[1-9]{1}\\d*$");
        Matcher match = pattern.matcher(str);
        return match.matches();
    }

    /**
     * 正则校验大于0正整数 最多n位数字
     * @param str
     * @return
     */
    public static boolean isPositiveNumber(String str, int num){
        if(num < 1){
            return false;
        }
        Pattern pattern = Pattern.compile("^[1-9]{1}\\d{0," + (num - 1) + "}$");
        Matcher match = pattern.matcher(str);
        return match.matches();
    }

    /**
     * 正则校验大于等于0正整数 最多n位数字
     * @param str
     * @return
     */
    public static boolean isPositiveNumberGeZero(String str, int num){
        Pattern pattern = Pattern.compile("^([0]{1})|([1-9]{1}\\d{0," + (num - 1) + "})$");
        Matcher match = pattern.matcher(str);
        return match.matches();
    }

    /**
     * 正则校验 4位汉字
     * @param str
     * @return
     */
    public static boolean isChinese_4(String str){
        Pattern pattern = Pattern.compile("^[\\u4e00-\\u9fa5]{1,4}$");
        Matcher match = pattern.matcher(str);
        return match.matches();
    }

    public static void main(String[] args) {
        String str = "99999";
        System.out.println(isPositiveNumberGeZero(str, 5));
    }

}
